<%-- 
    Document   : dstaikhoan
    Created on : Oct 14, 2011, 4:13:34 PM
    Author     : Nautilus
--%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="pojo.TaiKhoanPoJo" %>
<%@ page import="dao.TaiKhoanDao" %>
<%
	String s = request.getParameter("fullname")==null?"":request.getParameter("fullname");
	String usrname = request.getParameter("username")==null?"":request.getParameter("username");
	int pageIndex = (request.getParameter("pi")==null || request.getParameter("pi").equals(""))?1:Integer.parseInt(request.getParameter("pi"));
	int pageSize = request.getParameter("ps")==null?5:Integer.parseInt(request.getParameter("ps"));
	int[] totals = new int[1];
	TaiKhoanDao dao = new TaiKhoanDao();
	List<TaiKhoanPoJo> lsthoa = dao.timTaiKhoan(usrname, pageSize,pageIndex-1, totals);
	int n = lsthoa.size();
	//System.out.println("So khach hang tim duoc " + totals[0]);
	
	int id = request.getParameter("id")==null?0:Integer.parseInt(request.getParameter("id"));
	TaiKhoanPoJo selected = null;
	if(id>0)
	{
		selected = dao.layTaiKhoanTheoMa(usrname);
	}
	
	request.setCharacterEncoding("UTF-8");
	
%>

<script type='text/javascript'>
<!--
	 
	function next()
	{
		document.location.href = '<%=request.getContextPath()%>/index.jsp?f=10&fullname=<%=s%>&username=<%=usrname%>&pi=<%=(pageIndex+1)%>';
	}
	
	function previous()
	{
		document.location.href='<%=request.getContextPath()%>/index.jsp?f=10&fullname=<%=s%>&username=<%=usrname%>&pi=<%=(pageIndex-1)%>';
	}
	
	function onPageSizeChanged()
	{
		var ps=document.getElementById("cboPageSize").value;
		//alert(ps);
		document.location.href="<%=request.getContextPath()%>/index.jsp?f=10&fullname=<%=s%>&username=<%=usrname%>&pi=1&ps=" + ps;
	}
//-->
</script>


<center>
	<table>
		<tr>
			<td>
				<form id="frmUser" name="frmUser" method="POST" action="DangKyServlet">
					<table>
						<tr>
							<td>Tên Khách hàng: </td><td><input type="text" name="fullname" value='<%=selected==null?"":selected.getTenDangNhap()%>' />
							<input type='hidden' name='f' value='10'>
							<input type='hidden' name='id' value='<%=id%>'>
							</td>
						</tr>
						<tr>
							<td>Username: </td><td><input type="text" name="username" value='<%=selected==null?"":selected.getEmail()%>' /></td>
						</tr>
						<tr>
							<td>Email: </td><td><input type="text" name="email" value='<%=selected==null?"":selected.getEmail()%>' /></td>
						</tr>
						<tr>
							<td>Password: </td><td><input type="password" name="password" /></td>
						</tr>
						<tr>
							<td align="right" colspan="2"><input type="submit" value='Lưu cập nhật' /> <input type="button" value="Tìm kiếm" /></td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<table>
					<thead>
						<tr>
							<th>STT</th>
							<th>Họ Tên</th>
							<th>Username</th>
							<th>Email</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
						<%
							for(int i=0;i<n;i++)
							{
						%>
						<tr>
							<td><%=i%></td>
							<td><%=lsthoa.get(i).getTenDangNhap()%></td>
							<td><%=lsthoa.get(i).getEmail()%></td>
							<td><a href="<%=request.getContextPath()%>/index.jsp?f=10&id=<%=lsthoa.get(i).getIdTaiKhoan()%>&pi=<%=pageIndex%>&ps=<%=pageSize%>">reset password</a></td>
						</tr>
						<%
							}
						%>
					</tbody>
					<tfoot>
						<tr>
							<th colspan="5" align="right">
								Hiển thị &nbsp;
							<select id="cboPageSize" onchange="onPageSizeChanged()">
								<option value='5' <%=(pageSize==5?"selected":"")%>>5</option>
								<option value='10' <%=(pageSize==10?"selected":"")%> >10</option>
								<option value='15' <%=(pageSize==15?"selected":"")%> >15</option>
								<option value='20' <%=(pageSize==20?"selected":"")%> >20</option>
							</select>&nbsp; record(s) &nbsp; &nbsp; &nbsp;
							Trang <%=pageIndex%>/<%=(totals[0]/pageSize+1) %>
							&nbsp; &nbsp; &nbsp;
							<%
								if( (totals[0]>pageSize) && (pageIndex>1))
									out.print("<a onclick=\"previous()\" value=\"trước\" ><img src=\"images/previous-right.png\" ></a>");
									//out.print("<input type=\"button\" onclick=\"previous()\" value=\"trước\" />"); 
							
								if((totals[0]>pageSize) && (pageIndex < (totals[0]/pageSize+1) ))
									out.print("<a onclick=\"next()\" value=\"tiếp\" ><img src=\"images/next-right.png\" ></a>");
									//out.print("<input type=\"button\" onclick=\"next()\" value=\"tiếp\" />");
							%>
							</th>
						</tr>
					</tfoot>
				</table>
			</td>
		</tr>
	</table>
</center>